//index.js
//获取应用实例

const mockData = {
    money: 100,
    list: [
        {
            id: '0',
            status: 11,
            type_name: '开工',
            size: 123,
            address: '不知道',
            starttime: 232323,
            endtime: 29382,
            username: '好的',
            phone: '1212',
            avatar: null,
            createtime: 10000,
            supplier_name: '好啊',
            supplier_phone: '121234',
            supplier_avatar: null,
            total_price: 100
        },

        {
            id: '0',
            status: 10,
            type_name: '开工',
            size: 123,
            address: '不知道',
            starttime: 232323,
            endtime: 29382,
            username: '好的',
            phone: '1212',
            avatar: null,
            createtime: 10000,
            supplier_name: '好啊',
            supplier_phone: '121234',
            supplier_avatar: null,
            total_price: 100
        }

    ]
}

const app = getApp();

Page({
    data: {
        showNav: false,
        animationData: {},
        animationDatas: {},
        showSusses: false,
        showComplaint: false,
        page: 1,
        pageSize: 10,
        list: [],
        info: {},
        money: 0, //用户余额
        total_price: 0, //订单价格
        maxScore: 5,
        score: 0, //订单评分
        currentOrderNum: null,
        selectedCoupon: null
    },
    onLoad: function () {
        this.getList();
        this.setData({
            money: app.globalData.userInfo.money
        })
    },

    onShow(){
       this.upCoupon();
       this.methodPrice();
    },

    methodPrice(){

        
        let { selectedCoupon } = this.data;
        let info = this.data.info;
        let price = info.total_price;
        let end = selectedCoupon ? price - selectedCoupon.money : price;

        end = end > 0 ? end : 0;
        info.total_price = end;

        this.setData({
            info:info
        })
        
    },
    
    onHide: function () {

    },

    // 更行优惠券信息
    upCoupon(){
        let { selectedCoupon } = app.globalData;
        if(selectedCoupon){
            this.setData({
                selectedCoupon: selectedCoupon
            })
        }
    },
    
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.data.page = 1;
        this.getList();
    },
    
    // 到底部
    onReachBottom: function () {

    },

    // 清除选中优惠券
    clearSelectedCoupon(){

        this.setData({
            selectedCoupon: null
        })
        
    },

    onNav: function (e) {

        let that = this;
        let orderId = e.target.dataset.orderid;

        this.setCurrentOrderNum(e)

        // 清除选中优惠券
        this.clearSelectedCoupon();
        
        var info;
        
        for (var i = 0; i < this.data.list.length; i++) {
            
            let item = this.data.list[i];
            
            if (this.data.list[i]['id'] == orderId) {
                info = this.data.list[i];
            }
        }

        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: 'linear'
        })

        that.animation = animation
        animation.translateY(690).step();

        
        that.setData({
            animationData: animation.export(),
            showNav: true,
            info: info,
        });
        
        setTimeout(function () {
            animation.translateY(0).step()
            that.setData({
                animationData: animation.export(),
            })
        }, 200)
        
    },
    onGetCode: function (e) {
        let that = this;
        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: 'linear'
        })
        that.animation = animation
        animation.translateY(690).step();
        that.setData({
            animationDatas: animation.export(),
            showSusses: e.detail
        });
        setTimeout(function () {
            animation.translateY(0).step()
            that.setData({
                animationDatas: animation.export(),
            })
        }, 200)
    },

    // 确认完工
    suredone: function (e) {
        var _this = this;
        var orderId = e.currentTarget.dataset.id;
        var object = {
            url: app.api.url.order.orderSuredone,
            data: {order_id: orderId},
            success: function (res) {
                if (res.status == 1) {
                    wx.startPullDownRefresh();
                } else {
                    app.showToast(res.info);
                }
            }
        }
        
        this.setCurrentOrderNum(e);
        
        app.request(object)
    },

    // 提交评分
    submitComplaint(){
        
        let orderData = this.data.list[this.data.currentOrderNum];
        let _this = this;
        
        app.request({
            url: app.api.url.order.comment,
            data: {
                order_id: orderData.id,
                star: this.data.score,
            },
            complete (res){
                _this.switchComplaint(null, false);
            }
        })

    },

    showComplaint(e){
        this.switchComplaint(e, true);
        this.setCurrentOrderNum(e)
    },

    setCurrentOrderNum(e){
        let {index} = e.currentTarget.dataset;
        this.setData({
            currentOrderNum: index
        })
    }, 

    switchComplaint(e, type=undefined){
        this.setData({
            showComplaint: type === undefined ? !this.data.showComplaint : type,
            score: 0,
        })
    },

    toComplaint(e){

        let score = e.target.dataset.index;
        let _this = this;
        this.setData({
            score,
        })
    },

    // 投诉建议
    feedback: function (e) {
        var orderId = e.currentTarget.dataset.id;
        this.setCurrentOrderNum(e);
        app.navigateTo(app.page.Complaint, 'order_id=' + orderId);
    },
    
    // 订单详情
    orderDetail: function (e) {
        var orderId = e.currentTarget.dataset.id;
        app.navigateTo(app.page.orderDetail, 'id=' + orderId);
    },

    /**
     * 获取列表
     * @param combine 是否加载更多
     */
    getList: function (combine) {
        var _this = this;
        var object = {
            url: app.api.url.order.orderList,
            data: {
                page: _this.data.page, 
                pageSize: _this.data.pageSize, 
                status: '10,11,12,13,14,15'
            },
            complete: function () {
                wx.stopPullDownRefresh() //停止下拉刷新
            }
        }


        app.request(object).then(res =>{

            if (res.status == 1) {
                var list = res.data;
                for (var i = 0; i < list.length; i++) {
                    list[i]['starttime'] = list[i]['starttime'] > 0 ? app.util.dateTime(list[i]['starttime'], 'm/d') : '-';
                    list[i]['endtime'] = list[i]['endtime'] > 0 ? app.util.dateTime(list[i]['endtime'], 'm/d') : '-';
                }
                _this.setData({
                    list: list
                });
            } else {
                app.showToast(res.info);
            }
            
        });
    },
    closeSucess: function () {
        this.setData({
            showSusses: false
        });
    }
});
